package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import com.google.gson.Gson;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @Description:
 * @Author: Gavin
 * @Date: 9/11/2023 4:05 PM
 */
@RequestMapping("/customer")
@Api(tags = "客户管理模块")
@RestController
public class CustomerController {
    @Autowired
    CustomerService customerService;
    @ApiOperation("分页查询客户信息")
    @PostMapping("list")
    // @ResponseBody
    public String queryPageSuppliers(@RequestParam(value = "page",required = false)Integer page,
                                     @RequestParam(value = "rows",required = false)Integer rows,
                                     //将供应商的名字这个参数修改为非必须的,这样没有这个名字的时候也可以查询
                                     @RequestParam(value = "supplierName",required = false)String customerName){

        Map<String,Object> map = customerService.queryPageCustomers(page,rows,customerName);
        Gson gson = new Gson();
        return gson.toJson(map);
    }

    @ApiOperation("供应商的添加或修改")
    @PostMapping("/save")
    //id这个参数不是必须的,所以需要设置为required = false
    public ServiceVO saveOrUpdateSupplier(@RequestParam(value = "customerId",required = false)Integer customerId,
                                          Customer customer){
        //新增的时候没有id,所以不会传递id过来
        //需要确认下保存的时候需要供应商的id吗? 会传id过来

        //判断下,如果id不为空,那么就是修改,如果id为空,那么就是新增
        if (customerId == null){
            //进来这里面说明是新增的操作
            customerService.save(customer);
        }else{
            //进来这里面说明是更新的操作
            Integer count = customerService.update(customerId,customer);
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }

    @ApiOperation("根据id删除供应商,支持批量删除")
    @PostMapping("/delete")
    public ServiceVO deleteSupplierByIds(@RequestParam("ids")String ids){
        customerService.deleteCustomersByIds(ids);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }
}
